package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorBoutiqueRecommend;
import guoanmaker.operator.business.model.OutOperatorBoutiqueRecommend;
import guoanmaker.operator.business.model.ProvideCommodity;
import guoanmaker.operator.business.model.ProvideCommodityServiceRange;
import guoanmaker.operator.business.model.ProvideRoom;
import guoanmaker.operator.business.modelnobind.ProvideCommodityMove;
import guoanmaker.operator.business.modelnobind.ProvideCommodityServiceMove;
import guoanmaker.operator.business.repository.OperatorBoutiqueRecommendRepository;
import guoanmaker.operator.business.repository.OperatorRoomRepository;
import guoanmaker.operator.business.repository.ProvideCommodityRepository;
import guoanmaker.operator.business.repository.ProvideCommodityServiceRangeRepository;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.util.JsonUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Service
public class OperatorBoutiqueRecommendService implements OperatorBoutiqueRecommendServiceInterface {
	@Autowired
	private OperatorBoutiqueRecommendRepository operatorBoutiqueRecommendRepository;
	@Autowired
	private AjaxJson ajaxJson;
	@Autowired
	private OperatorRoomRepository operatorRoomRepository;
	@PersistenceContext
	private EntityManager manager;
	@Autowired
	private ProvideCommodityRepository provideCommodityRepository;
	@Autowired
	private ProvideCommodityServiceRangeRepository provideCommodityServiceRangeRepository;
	@Override
	@Transactional
	public OperatorBoutiqueRecommend saveOperatorBoutiqueRecommend(OperatorBoutiqueRecommend operatorBoutiqueRecommend) {
		// TODO Auto-generated method stub
		return operatorBoutiqueRecommendRepository.save(operatorBoutiqueRecommend);
	}
	@Override
	public List<OutOperatorBoutiqueRecommend> selectOperatorBoutiqueRecommends(String cityCode,Integer page, Integer size) {
		// TODO Auto-generated method stub
		Pageable pageable = new PageRequest(page, size);
		Page<OperatorBoutiqueRecommend> ofls=operatorBoutiqueRecommendRepository.findByCityCode(cityCode,pageable);
	        List<OutOperatorBoutiqueRecommend> outobrs=new ArrayList<OutOperatorBoutiqueRecommend>();
	        
	        for(OperatorBoutiqueRecommend obr:ofls){
	        	OutOperatorBoutiqueRecommend outOperatorBoutiqueRecommend=new OutOperatorBoutiqueRecommend();
	        	try{
	        	if(obr.getProvideCommodityId().contains("S")){
	        		ProvideRoom provideRoom=operatorRoomRepository.findByroomCodes(obr.getProvideCommodityId());
	        		
					outOperatorBoutiqueRecommend.setCommitName(provideRoom.getRoomName());
					outOperatorBoutiqueRecommend.setCommitid(provideRoom.getRoomCodes());
					outOperatorBoutiqueRecommend.setCommitUrl(provideRoom.getRoomlogo());
					outOperatorBoutiqueRecommend.setSynopsis(provideRoom.getDescribeAsk());
					outOperatorBoutiqueRecommend.setServerType(obr.getServerType());
					
	        	}else{
	        		
	        		String jpql="select new guoanmaker.operator.business.modelnobind.ProvideCommodityMove( "
	        				+ "t.commodityName,b.businessName,t.isBargaining,t.startingPrice,d.dictName,t.invoiceMail,t.isTerritoryRestriction"
	        				+ ",t.commodityPrice,t.commodityPricePromotion,t.startTime,t.endTime,t.commodity_bigimage,t.description,t.commodityState,t.releaseTime,t.stock"
	        				+ ",t.populationRestriction,t.registrationStartTime,t.registrationEndTime,t.synopsis,t.id,t.type,sv.serverName,t.isPromotion,t.clickRate,t.process"
	        				+ ",t.promise,t.commodityCase,t.auditOpinion,t.finalPrice,t.introduce,t.commodity_smallimage,b.LicensPhone,t.commodityCode) "
	        				+ " from ProvideCommodity t  left  join t.providerBusiness b left join t.serviceType  sv"
	        				+ " left join t.measurementUnit d    where t.commodityState=2 and  t.commodityCode=:commodityCode and t.type!=1";
	        		Integer code=Integer.parseInt(obr.getProvideCommodityId());
	        		Query query=manager.createQuery(jpql);//查询商品信息
	        		query.setParameter("commodityCode", code);
	        		if(query.getFirstResult()==0){}
	        		List<ProvideCommodityMove> comms= query.getResultList();
	        		System.out.println("长度"+comms.size());
	        		ProvideCommodityMove commodity=null;
	        		if(comms.size()>0){
	        			System.out.println("进去了");
	        			commodity=comms.get(0);
	        			System.out.println(commodity.getCommodityCode()+"code码");
	        			String jpql2="select new guoanmaker.operator.business.modelnobind.ProvideCommodityServiceMove(s.id,ct.code,a.name,ct.name,p.name,ct.provincecode,s.isPromotion,s.commodityPrice,s.commodityPricePromotion,s.startTime,s.endTime,s.finalPrice)"
	        					+ " from ProvideCommodityServiceRange s left join  s.commodity c left join s.city ct left join s.area a left join s.province p  where  c.commodityCode=:commodityCode and ct.code=:code order by s.finalPrice";
	        			Query query2=manager.createQuery(jpql2);//查询商品提供的服务区域
	        			query2.setParameter("commodityCode", code);
	        			query2.setParameter("code", cityCode);
	        			List<ProvideCommodityServiceMove> lists=query2.getResultList();
	        			commodity.setServiceRange(lists);
	        			if(lists.size()>0){
	        				commodity.setFinalPrice(lists.get(0).getFinalPrice());
	        			}
	        		}
	        		
	        		System.out.println(commodity.getCommodityCode()+"商品id");
	        		outOperatorBoutiqueRecommend.setCommitFinnalPrice(commodity.getFinalPrice());
					outOperatorBoutiqueRecommend.setCommitName(commodity.getCommodityName());
					outOperatorBoutiqueRecommend.setCommitid(commodity.getCommodityCode()+"");
					outOperatorBoutiqueRecommend.setCommitUrl(commodity.getCommodity_smallimage());
					outOperatorBoutiqueRecommend.setSynopsis(commodity.getSynopsis());
					outOperatorBoutiqueRecommend.setServerType(obr.getServerType());
					
	        	        	}
	        	
	        	outobrs.add(outOperatorBoutiqueRecommend);
	        	}catch(Exception e){
	        		continue;
	        		
	        	}	
			}
		
		return outobrs;
	}
	@Override
	public List<OperatorBoutiqueRecommend> selectOperatorBoutiqueRecommendsPc(String cityCode) {
		// TODO Auto-generated method stub
		return operatorBoutiqueRecommendRepository.findByCityCodePc(cityCode);
	}
	@Override
	public ProvideCommodity selectProvideCommodity(Integer commodityCode) {
		
		// TODO Auto-generated method stub
		return provideCommodityRepository.selectByCommodityBypraThree(commodityCode);
	}
	@Override
	public List<ProvideCommodityServiceRange> selectProvideCommodityServiceRange(String id, String cityId) {
		// TODO Auto-generated method stub
		return provideCommodityServiceRangeRepository.selectBycityandid(id, cityId);
	}
	@Override
	@Transactional
	public void deleteOutOperatorBoutiqueRecommend(String id) {
		// TODO Auto-generated method stub
		operatorBoutiqueRecommendRepository.delete(id);
	}
	
	 @Transactional
	  public void delectAllBycitycode(String cityCode){
		  String jpql="delete  from OperatorBoutiqueRecommend  u where u.cityCode=:cityCode";
		  Query query2=manager.createQuery(jpql);//查询商品提供的服务区域
			query2.setParameter("cityCode", cityCode);
			query2.executeUpdate();
		  
	  }

}
